package com.android.gilastv.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.dialog.Comm_DialogFragment;
import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.tool.Log;
import com.android.baselibrary.util.GlideUtil;
import com.android.baselibrary.util.Util;
import com.android.baselibrary.view.GridSpacingItemDecoration;
import com.android.baselibrary.view.RoundImageView;
import com.android.gilastv.R;
import com.android.gilastv.adapter.CommVideoListAdapter;
import com.android.gilastv.bean.CommVideoInfo;
import com.android.gilastv.bean.HistoryInfo;
import com.android.gilastv.bean.UserInfo;
import com.android.gilastv.constants.Constants;
import com.android.gilastv.manager.API_Manager;
import com.android.gilastv.manager.UserManager;
import com.android.gilastv.responce.DianZanResponce;
import com.android.gilastv.ui.fragment.SettingFragment;
import com.android.gilastv.ui.view.Comm_HeadStateView;
import com.google.gson.Gson;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyActivity extends BaseActivity {

    @BindView(R.id.radiogroup_repeat)
    RadioGroup radiogroup_repeat;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.iv_head)
    RoundImageView iv_head;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.head_stateview)
    Comm_HeadStateView head_stateview;


    SettingFragment fragment1;

    CommVideoListAdapter adapter;
    List<CommVideoInfo> list = new ArrayList<>();
    int page = 1;
    int size = 30;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my;
    }

    @Override
    protected void initView() {
        head_stateview.setLeftClickFinish(this);
        fragment1 = (SettingFragment) getSupportFragmentManager().findFragmentById(R.id.fragment1);

        recyclerview.setLayoutManager(new GridLayoutManager(mContext, 5));
        recyclerview.addItemDecoration(new GridSpacingItemDecoration(5, Util.dip2px(mContext, 10), true));
        adapter = new CommVideoListAdapter(mContext, null);
        recyclerview.setAdapter(adapter);

        radiogroup_repeat.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                switch (checkedId) {
                    case R.id.tb_repeat1:
                        getHistory();  //历史记录
                        setFragmentVisible(false);
                        break;
                    case R.id.tb_repeat2:
                        queryUserCollect();  //收藏
                        setFragmentVisible(false);
                        break;
                    case R.id.tb_repeat3:
                        queryUserLike();   //点赞
                        setFragmentVisible(false);
                        break;
                    case R.id.tb_repeat4:
                        setFragmentVisible(true); //设置
                        break;
                }
            }
        });


        setFragmentVisible(false);
    }

    //显示影藏 设置
    private void setFragmentVisible(boolean isVisible) {

        if (isVisible) {
            getSupportFragmentManager().beginTransaction().show(fragment1).commit();
            recyclerview.setVisibility(View.GONE);
        } else {
            getSupportFragmentManager().beginTransaction().hide(fragment1).commit();
            recyclerview.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initData() {
        getHistory();  //历史记录
        setFragmentVisible(false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (UserManager.isLogin(mContext)) {
            getUserInfo();
        }
    }

    //刷新用户信息
    private void getUserInfo() {
        API_Manager.getUserInfo(mContext, new OkHttpCallBack<BaseResponce<UserInfo>>() {
            @Override
            public void onSuccess(BaseResponce<UserInfo> baseResponce) {
                UserInfo userInfo = baseResponce.getData();
                UserManager.saveUserInfo(mContext, baseResponce.getData());

                tv_name.setText(userInfo.nickname);
                GlideUtil.displayImage(mContext, CommVideoInfo.getCoverUrl(userInfo.headId), iv_head, R.drawable.ico_default_head);
            }

            @Override
            public void onFailure(BaseResponce<UserInfo> baseResponce) {
                CommToast.showToast(mContext, baseResponce.msg);
            }
        });
    }

    //获取收藏列表
    private void queryUserCollect() {
        API_Manager.queryUserCollect(mContext, page, size, new OkHttpCallBack<BaseResponce<List<DianZanResponce>>>() {
            @Override
            public void onSuccess(BaseResponce<List<DianZanResponce>> baseResponce) {
//                smartrefreshlayout.finishRefresh();
//                smartrefreshlayout.finishLoadMore();
                list.clear();
                for (DianZanResponce responce : baseResponce.getData()) {
                    if (responce.recvideo != null)
                        list.add(responce.recvideo);
                }
                adapter.setData(list);
//                if (adapter.getItemCount() == 0) {
//                    multiplestatusView.setViewState(MultiStateView.ViewState.EMPTY);
//                } else {
//                    multiplestatusView.setViewState(MultiStateView.ViewState.CONTENT);
//                }
            }

            @Override
            public void onFailure(BaseResponce<List<DianZanResponce>> baseResponce) {
//                smartrefreshlayout.finishRefresh();
//                smartrefreshlayout.finishLoadMore();
//                multiplestatusView.setViewState(MultiStateView.ViewState.ERROR);
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }

    //获取点赞列表
    private void queryUserLike() {
        API_Manager.queryUserLike(mContext, page, size, new OkHttpCallBack<BaseResponce<List<DianZanResponce>>>() {
            @Override
            public void onSuccess(BaseResponce<List<DianZanResponce>> baseResponce) {
//                smartrefreshlayout.finishRefresh();
//                smartrefreshlayout.finishLoadMore();
//                adapter.setData(baseResponce.getData().list);
//                if (adapter.getItemCount() == 0) {
//                    multiplestatusView.setViewState(MultiStateView.ViewState.EMPTY);
//                } else {
//                    multiplestatusView.setViewState(MultiStateView.ViewState.CONTENT);
//                }
                list.clear();
                for (DianZanResponce responce : baseResponce.getData()) {
                    if (responce.recvideo != null)
                        list.add(responce.recvideo);
                }
                adapter.setData(list);
            }

            @Override
            public void onFailure(BaseResponce<List<DianZanResponce>> baseResponce) {
//                smartrefreshlayout.finishRefresh();
//                smartrefreshlayout.finishLoadMore();
//                multiplestatusView.setViewState(MultiStateView.ViewState.ERROR);
                CommToast.showToast(mContext, Constants.NetError);
            }
        });
    }

    //获取历史记录
    private void getHistory() {
        List<HistoryInfo> historyInfos = LitePal.findAll(HistoryInfo.class);
        Log.i("historyInfos:" + historyInfos.size());
        list.clear();
        for (HistoryInfo historyInfo : historyInfos) {
            list.add(new Gson().fromJson(historyInfo.json, CommVideoInfo.class));
        }
        adapter.setData(list);
    }

    @OnClick({R.id.iv_head, R.id.tv_back,R.id.tv_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_head:      //跳登录
                if(!UserManager.isLogin(mContext))
                {
                    startActivity(new Intent(mContext, LoginActivity.class));
                }
                break;
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_logout:
                Comm_DialogFragment.showCommDialog((FragmentActivity) mContext, "", "确定退出登录?", "确定", "取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UserManager.logout(mContext);
                        finish();
                    }
                }, null);
                break;
        }
    }
}
